package demo15_1;

import javax.swing.plaf.IconUIResource;

public class MySingleList {
    static class ListNode {
        public int value;//节点的值域
        public ListNode next;//下一个节点的地址
        public ListNode() {

        }
        public ListNode(int value) {
            this.value = value;
        }
    }

    public ListNode head;//表示当前链表的头节点

    //这种链表的创建方式不太好
    public void createList() {
        //只创建了5个节点，之间不存在关系
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(34);
        ListNode node3 = new ListNode(56);
        ListNode node4 = new ListNode(78);
        ListNode node5 = new ListNode(91);
        //将5个节点连接起来
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        //头节点指向第一个节点
        this.head = node1;
        //当createList执行完毕，局部变量note1,note2,note3,note4,note5都被回收，但是成员变量head已经成为了第一个节点
    }

    // 1、无头单向非循环链表实现
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
            return;
        }
        ListNode p = this.head;
        while (p.next != null) {
            p = p.next;
        }
        p.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index < 0 || index > size()) {
            throw new IndexErrorException("index不合法");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        //找到要删除结点的前一个节点
        ListNode p = findIndexSub(index);
        node.next = p.next;
        p.next = node;
    }
    //找到要删除结点的前一个节点
    private ListNode findIndexSub(int index) {
        ListNode p = head;
        while (index - 1 != 0) {
            p = p.next;
            index --;
        }
        return p;
//        for (int i = 0; i < index - 1; i++) {
//            p = p.next;
//        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode p = this.head;
        while (p != null) {
            if (p.value != key) {
                p = p.next;
            }else {
                return true;
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (head == null) {
            System.out.println("链表为空");
            return;
        }
        if (head.value == key) {
            head = head.next;
            return;
        }
        //找到要删除节点的前一个节点
        ListNode p = searchPrev(key);
        if (p == null) {
            System.out.println("没有你要找的节点");
            return;
        }
        ListNode del = p.next;
        p.next = del.next;
    }
    //找到要删除节点的前一个节点
    private ListNode searchPrev(int key) {
        ListNode p = head;
        //找到要删除节点的前一个节点
        while (p.next != null) {
            if (p.next.value == key) {
                return p;
            }
            p = p.next;
        }
        return null;
    }
    public ListNode removeElements(ListNode head, int val) {
        if (this.head == null) {
            System.out.println("链表为空");
            return null;
        }
        //没有判断头节点
        //表示当前要删除的节点
        ListNode cur = this.head.next;
        //表示要删除节点的前驱
        ListNode prev = this.head;
        while (cur != null) {
            if (cur.value == val) {
                prev.next = cur.next;
            }else {
                prev = cur;
            }
            cur = cur.next;
        }

        //最后单独处理头节点
        if (this.head.value == val) {
            this.head = this.head.next;
        }
        return head;
    }
    //删除所有值为key的节点，只遍历一遍
    public void removeAllKey(int key){
        if (this.head == null) {
            System.out.println("链表为空");
            return;
        }
        //没有判断头节点
        //表示当前要删除的节点
        ListNode cur = this.head.next;
        //表示要删除节点的前驱
        ListNode prev = this.head;
        while (cur != null) {
            if (cur.value == key) {
                prev.next = cur.next;
            }else {
                prev = cur;
            }
            cur = cur.next;
        }

        //最后单独处理头节点
        if (this.head.value == key) {
            this.head = this.head.next;
        }

    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode p = this.head;
        while (p != null) {
            count++;
            p = p.next;
        }
        return count;
    }
    public void clear() {
        this.head = null;
    }
    public void display() {
        ListNode p = this.head;
        //p==null证明把链表遍历完成了
        while (p != null) {
            System.out.print(p.value+" ");
            //p向后走一步
            p = p.next;
        }
        System.out.println();
    }
    //从指定的位置开始打印
    public void display(ListNode head) {
        ListNode p = this.head;
        //p==null证明把链表遍历完成了
        while (p != null) {
            System.out.print(p.value+" ");
            //p向后走一步
            p = p.next;
        }
        System.out.println();
    }

    //原地反转链表
    public ListNode reverseList() {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        ListNode cur = head.next;

        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    //只遍历一遍，找到中间节点
    //fast一次走两步，slow一次走一步
    public ListNode middleNode() {
        ListNode slow = head;
        ListNode fast = head;
        //fast.next可能会空指针异常
        //把两个条件顺序换一下fast.next != null && fast != null
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    public int kthToLast(ListNode head, int k) {
//        if (k < 0 || k > size()) {
//            return null;
//        }
        ListNode slow = head;
        ListNode fast = head;
        for (int i = 0; i < k - 1; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow.value;
    }


//分割
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;
        while (cur != null) {
            if (cur.value < x) {
                if(bs == null) {
                    bs = be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if(as == null) {
                    as = ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }
//回文
    public boolean chkPalindrome(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        //slow指向中间节点
        //翻转slow后的节点
        ListNode cur = slow.next;

        while(cur != null) {
            ListNode curNext = cur.next;//记录下一个节点
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //翻转完成 slow指向最后一个节点
        while(head != slow) {
            if(head.value != slow.value) {
                return false;
            }
            //偶数情况
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
    //相交



}
